package com.ruoyi.stock2.thread.aboutDownloadData;
//package com.ruoyi.analysis.thread;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.math.BigDecimal;
//import java.util.List;
//import org.jsoup.Jsoup;
//import org.jsoup.Connection.Response;
//import org.jsoup.nodes.Document;
//import org.jsoup.nodes.Element;
//import org.springframework.util.StringUtils;
//
//import com.ruoyi.analysis.domain.DownTradedataSchedule;
//import com.ruoyi.analysis.domain.STOCK;
//import com.ruoyi.analysis.domain.TradeData;
//import com.ruoyi.analysis.domain.TradeDataKey;
//import com.ruoyi.analysis.mapper.DownTradedataScheduleMapper;
//import com.ruoyi.analysis.mapper.STOCKMapper;
//import com.ruoyi.analysis.mapper.TradeDataMapper;
//import com.ruoyi.analysis.service.UpdateStocksSymbolDataService;
//import com.ruoyi.analysis.thread.aboutDownloadData.CheckSourceReady1;
//import com.ruoyi.analysis.utils.DateUtils2;
//import com.ruoyi.common.utils.spring.SpringUtils;
//
////数据从sina下载 目前接口被封
//@Deprecated
//public class DownLoadStockTradeDataThread2 {
//	
//	DownTradedataScheduleMapper downLoadTradedataScheduleMapper;
//	STOCKMapper stockMapper;
//	TradeDataMapper tradedataMapper;
//	UpdateStocksSymbolDataService updateStocksSymbolDataService;
//	
//	//单例实现 涉及到任务状态
//	public static DownLoadStockTradeDataThread2 getInstance() {
//		return Singtonle.instance;
//	}
//
//	private static class Singtonle {
//		private static DownLoadStockTradeDataThread2 instance = new DownLoadStockTradeDataThread2();
//	}
//    
//	public DownTradedataScheduleMapper getDownLoadTradedataScheduleMapper() {
//		return (DownTradedataScheduleMapper)SpringUtils.getBean(DownTradedataScheduleMapper.class);
//	}
//
//	public STOCKMapper getStockMapper() {
//		return (STOCKMapper)SpringUtils.getBean(STOCKMapper.class);
//	}
//
//	public TradeDataMapper getTradedataMapper() {
//		return (TradeDataMapper)SpringUtils.getBean(TradeDataMapper.class);
//	}
//	
//	public UpdateStocksSymbolDataService getUpdateStocksSymbolData() {
//		return (UpdateStocksSymbolDataService)SpringUtils.getBean(UpdateStocksSymbolDataService.class);
//	}
//	
//	boolean whileflag11 = true;
//	
//
//	public void run() {
//		//初始化实例
//		downLoadTradedataScheduleMapper = getDownLoadTradedataScheduleMapper();
//		tradedataMapper = getTradedataMapper();
//		stockMapper = getStockMapper();
//		updateStocksSymbolDataService = getUpdateStocksSymbolData();
//		
//		step1 ();
//		
//		downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(new DownTradedataSchedule("download_schedule","000000"));
//		downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(new DownTradedataSchedule("delete_tradedata","000000"));
//	}
//
//	public void step1 () {
//		//获取系统已经完成下载数据日期
//		String lastDownLoadDateFromSina = downLoadTradedataScheduleMapper.selectByPrimaryKey("lastDownLoadFromSinaDate").getValue();
//		String diyDownloadDate = downLoadTradedataScheduleMapper.selectByPrimaryKey("diyDownloadDate").getValue();
//		String nowSinaDate = null;
//		if(StringUtils.isEmpty(diyDownloadDate)) {
//			nowSinaDate = CheckSourceReady1.checkSourceReady();
//		}else {
//			nowSinaDate = diyDownloadDate;//如果不为空，表示是用户自己设定的日期
//		}
//		//获取sinajs已经完成数据日期
//		if (nowSinaDate.compareTo(lastDownLoadDateFromSina) > 0) {
//			
//			//中断后重新开始下载的处理 begin
//			DownTradedataSchedule ac = downLoadTradedataScheduleMapper.selectByPrimaryKey("download_schedule");
//			if(!ac.getValue().equals("000000")){
//				downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(new DownTradedataSchedule("delete_tradedata", "开始删除" + ac.getValue() + " " + lastDownLoadDateFromSina + "日及之后日期的数据"));
//				TradeDataKey tradeDatakey = new TradeDataKey();
//				tradeDatakey.setSymbol(ac.getValue());
//				tradeDatakey.setDatetime(DateUtils2.formatStringToDate(lastDownLoadDateFromSina, "yyyy-MM-dd"));
//				//删除股票代码大于等于symbol并且日期大于lastDownLoadDateFromSina 的数据
//				tradedataMapper.deleteByPrimaryKey(tradeDatakey);
//				downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(new DownTradedataSchedule("delete_tradedata", "完成删除" + ac.getValue() + " " + lastDownLoadDateFromSina + "日及之后日期的数据"));
//				
//			}
//			//中断后重新开始下载的处理 end
//			
//			downLoadTradeData(lastDownLoadDateFromSina, nowSinaDate);
//			
//			downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(new DownTradedataSchedule("lastDownLoadFromSinaDate",nowSinaDate));
//			
//		} else {
//			DownTradedataSchedule dts = new DownTradedataSchedule("sinadata_ready?","新浪交易历史数据未准备好");
//			dts.setRemark("新浪交易历史数据未准备好或者有自定义日期");
//			downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(dts);
//		}
//		
//	}
//
//	/*
//	 * 下载及补全日交易数据
//	 * http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/000014/type/S.phtml?year=2014&jidu=1
//	 * http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/399001/type/S.phtml?year=2004&jidu=1
//	 * 下面是搜狐的
//	 * //http://q.stock.sohu.com/hisHq?code=cn_000001&start=20170908&end=20170912&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp
//	 *   
//	 * http://q.stock.sohu.com/cn/600066/lshq.shtml
//	 * http://q.stock.sohu.com/cn/000002/lshq.shtml
//	 * http://q.stock.sohu.com/hisHq?code=cn_000002&start=20180712&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.2800235074816233&0.9731167585342846
//	 * http://q.stock.sohu.com/hisHq?code=cn_600066&start=20080712&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.09245341454166156&0.7706734285245993
//	 * http://q.stock.sohu.com/hisHq?code=cn_300003&start=20180712&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.6675299288802337&0.48050599358676505
//	 * 
//	 * http://q.stock.sohu.com/zs/399001/lshq.shtml //深圳成指
//	 * http://q.stock.sohu.com/zs/000001/lshq.shtml //上证指数
//	 * http://q.stock.sohu.com/zs/399006/lshq.shtml //创业指数
//	 * http://q.stock.sohu.com/hisHq?code=zs_399001&start=20180601&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.26095705673043135&0.19095589644024713
//	 * http://q.stock.sohu.com/hisHq?code=zs_000001&start=20180712&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.9898403714008159&0.9310353173932411
//	 * http://q.stock.sohu.com/hisHq?code=zs_399006&start=20180712&end=20181108&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.18585862433273292&0.5263393864001715
//	 */
//	public void downLoadTradeData(String lastDownLoadDateFromSina, String nowSinaDate) {
//
//		int nowfre = (Integer.valueOf(nowSinaDate.split("-")[1]) - 1) / 3 + 1;// 当前日期的季度
//		int nowyear = Integer.parseInt(nowSinaDate.split("-")[0]);// 当前日期的年份
//
//		int beginfre = (Integer.valueOf(lastDownLoadDateFromSina.split("-")[1]) - 1) / 3 + 1;// beginDate日期的季度
//		int beginyear = Integer.parseInt(lastDownLoadDateFromSina.split("-")[0]);// beginDate日期的年份
//		
//		List<STOCK> stocks = stockMapper.selectSTOCKList(null);//order by SYMBOL asc
//		DownTradedataSchedule ac = downLoadTradedataScheduleMapper.selectByPrimaryKey("download_schedule");
//		for (int k = 0; k < stocks.size(); k++) {// list遍历 非已退市
//			if(stocks.get(k).getSymbol().compareTo(ac.getValue())<0){
//				continue;
//			}
//			DownTradedataSchedule ac1 = downLoadTradedataScheduleMapper.selectByPrimaryKey("download_schedule");
//			ac1.setValue(stocks.get(k).getSymbol());
//			downLoadTradedataScheduleMapper.updateByPrimaryKeySelective(ac1);
//			
//			for (int i = Integer.parseInt(lastDownLoadDateFromSina.split("-")[0]); i <= nowyear; i++) {// 年份遍历
//				
//				for (int j = 1; j <= 4; j++) {// 从第一季度到第四季度
//					if (("" + i + j).compareTo("" + nowyear + nowfre) > 0) {
//						continue;// 大于当前年份季度的”过“
//					}
//					if (("" + i + j).compareTo("" + beginyear + beginfre) < 0) {
//						continue;// 小于beginDate年份季度的”过“
//					}
//					whileflag11 = true;
//					while (whileflag11) {// 循环调用下载程序直到成功
//						downloadData(stocks.get(k).getSymbol(), i + "", j + "", lastDownLoadDateFromSina, nowSinaDate);
//						//非全量下载时 使用的时间间隔
//						try {
//							Thread.sleep(4000);
//						} catch (InterruptedException e) {
//							e.printStackTrace();
//						}
//					}
//				}
//				/*if(i%2==0) {//全量下载时用
//					try {
//						Thread.sleep(6000);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}*/
//				
//			}
//		}
//	}
//	//private void downloadData(String symbol, String year, String fre, String lastDownLoadDateFromSina, String nowSinaDate) {
//	private void downloadData(String symbol, String lastDownLoadDateFromSina, String nowSinaDate) {
//		String code = "1"+symbol;;
//		if("999001".equals(symbol)) {
//			code = "1399001";
//		}
//		if("900001".equals(symbol)) {
//			code = "0000001";
//		}
//		if("999006".equals(symbol)) {
//			code = "1399102";
//		}
//		if(symbol.startsWith("6")) {
//			code = "0"+symbol;
//		}
//		BufferedReader bufferedReader = null;
//		try {
//			
//			Response resultImageResponse = Jsoup.connect("http://quotes.money.163.com/service/chddata.html?code="+code+"&start="+lastDownLoadDateFromSina+"&end="+nowSinaDate+"&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP")
//					.ignoreContentType(true)
//					.execute();
//			
//			whileflag11 = false;
//			bufferedReader = new BufferedReader(new InputStreamReader(resultImageResponse.bodyStream(), "GBK"));
//            String s;
//            //日期,        股票代码,  名称,  收盘价,最高价,最低价, 开盘价,前收盘,涨跌额, 涨跌幅, 换手率, 成交量, 成交金额, 总市值,       流通市值
//            //2005-12-28,'000014,沙河股份,0.0,  0.0, 0.0, 0.0, 6.41, None, None, 0.0, 0,    0.0,   574635667.5,289862449.91
//            bufferedReader.readLine();
//            while ((s = bufferedReader.readLine()) != null) {
//                String[] e = s.split(",");
//                if(!"None".equals(e[8]) && !"None".equals(e[9]) && !"0.0".equals(e[3])) {
////                	TradeData163 tradeData = new TradeData163();
////    				tradeData.setDatetime(DateUtils.formatStringToDate(e[0], "yyyy-MM-dd"));
////    				tradeData.setSymbol(symbol);
////    				tradeData.setDateopenprice(new BigDecimal(e[6]));
////    				tradeData.setCloseprice(new BigDecimal(e[3]));
////    				tradeData.setLowprice(new BigDecimal(e[5]));
////    				tradeData.setHighprice(new BigDecimal(e[4]));
////    				tradeData.setAmount(new BigDecimal(e[11]));
////    				tradeData.setVolume(new BigDecimal(e[12]));
////    				tradeData163Mapper.insert(tradeData);
//                }
//                
//            }
//            
//
//			whileflag11 = false;
//		} catch (java.net.SocketTimeoutException e1) {
//			e1.printStackTrace();
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		} finally {
//			try {
//				if(bufferedReader!=null) {
//					bufferedReader.close();
//				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
//	
//	private void downloadData(String symbol, String year, String fre, String lastDownLoadDateFromSina, String nowSinaDate) {
//		Document doc;
//		try {//http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/399006/type/S.phtml?year=2004&jidu=1
//			// http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/000001/type/S.phtml?year=2014&jidu=1
//			// http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/399001/type/S.phtml?year=2004&jidu=1
//			if(symbol.equals("900001")){//上证指数
//				doc = Jsoup.connect("http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/000001/type/S.phtml?year=" + year + "&jidu=" + fre).timeout(5000).get();
//			}else if(symbol.equals("999001")){//深圳成指
//				doc = Jsoup.connect("http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/399001/type/S.phtml?year=" + year + "&jidu=" + fre).timeout(5000).get();
//			}else if(symbol.equals("999006")){//创业板指
//				doc = Jsoup.connect("http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/399006/type/S.phtml?year=" + year + "&jidu=" + fre).timeout(5000).get();
//			}else{
//				// http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/000908.phtml
//				// http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/600179.phtml?year=2008&jidu=3
//				doc = Jsoup.connect("http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/" + symbol	+ ".phtml?year=" + year + "&jidu=" + fre).timeout(5000).get();
//			}
//			Element e = doc.getElementById("FundHoldSharesTable");
//			if (e != null) {
//				String[] str = e.text().split(" ");
//				for (int i = 7; i < str.length - 1; i = i + 7) {
//					// 2015-03-31 > 1900-01-01 && 2015-03-31 < 2016-05-07 项目初建时
//					// 2015-03-31为网页中的取值之一
//					// 2016-05-09 > 2016-05-06 && 2016-05-09 <=
//					// 2016-05-09交易数据天天更新的情况下
//					//System.out.println(str[i + 1] +" "+lastDownLoadDateFromSina +" "+ nowSinaDate + " " + (str[i + 1].compareTo(lastDownLoadDateFromSina) > 0 && str[i + 1].compareTo(nowSinaDate) <= 0));
//					if (str[i + 1].compareTo(lastDownLoadDateFromSina) > 0 && str[i + 1].compareTo(nowSinaDate) <= 0) {
//						fun_insert(str, i, symbol);
//					}
//				}
//			} else {
//				
//			}
//			// 下载成功后标志位更改，免得再次下载
//			whileflag11 = false;
//			System.out.println("competed : " + symbol + " " + year + "年" + fre + "季");
//		} catch (java.net.SocketTimeoutException e1) {
//			System.err.println("Read timed out : " + symbol + " " + year + "年" + fre + "季");
//		} catch (Exception e1) {
//			System.err.println("Exception : " + symbol + " " + year + "年" + fre + "季");
//		}
//	}
//	
//	
//	private void fun_insert(String[] str, Integer i, String symbol) {
//		TradeData tradeData = new TradeData();
//		tradeData.setDatetime(DateUtils2.formatStringToDate(str[i + 1], "yyyy-MM-dd"));
//		tradeData.setDateopenprice(new BigDecimal(str[i + 2]));
//		tradeData.setHighprice(new BigDecimal(str[i + 3]));
//		tradeData.setCloseprice(new BigDecimal(str[i + 4]));
//		tradeData.setLowprice(new BigDecimal(str[i + 5]));
//		tradeData.setVolume(new BigDecimal(str[i + 6]));
//		tradeData.setAmount(new BigDecimal(str[i + 7]));
//		tradeData.setSymbol(symbol);
//		TradeDataKey key = new TradeDataKey(symbol, tradeData.getDatetime());
//		TradeData tradeData1 = tradedataMapper.selectByPrimaryKey(key);
//		if(tradeData1 != null) {
//			tradedataMapper.updateByPrimaryKeySelective(tradeData);
//		}else {
//			tradedataMapper.insertSelective(tradeData);	
//		}
//		
//	}
//	
//
//	}
